


/*
*************************************************************************
*西瑞克斯研发管理系统
*版 本: 1.0   
*日 期：02/29/2012    
*作 者： 王浩 QQ：873987060   
*************************************************************************   
*/  
package com.application.yfplacefile.dao;
import java.util.List;
import java.util.ArrayList;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;

import com.manager.commons.CommonUtil;
import com.manager.commons.page.PageBean;
import org.apache.log4j.Logger;
import com.manager.commons.dao.HibernateGenericDao;
import com.manager.commons.page.PageBean;
import com.application.yfplacefile.entity.YfPlacefile;
import com.application.yfplacefile.struts.form.YfPlacefileForm;
 /**
 * 
 * Description:YfPlacefileDAO<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060 <br />
 * 
 * History: <br />
 * 
 * 02/29/2012 
 * 
 */
public class YfPlacefileDAO extends HibernateGenericDao<YfPlacefile>
{
    private static final Logger logger = Logger.getLogger(YfPlacefileDAO.class);

	private YfPlacefile yfPlacefile;

	public YfPlacefile getYfPlacefile() {
		return this.yfPlacefile;
	}

	public void setYfPlacefile(YfPlacefile yfPlacefile) {
		this.yfPlacefile = yfPlacefile;
	}

	public Integer getAllCountYfPlacefile(YfPlacefileForm yfPlacefileForm){
	    List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT COUNT(0) FROM yf_placefile t left join yf_project p on t.project_no = p.project_no left join department b on p.project_org = b.id  WHERE 1=1 ");
                sql = getStringBuffer(yfPlacefileForm,sql,args);
		return super.getCount(sql.toString(),args.toArray());
	}
        
	public void deleteYfPlacefiles(YfPlacefileForm yfPlacefileForm){    	
    		String hql = " delete from YfPlacefile where id = '" + yfPlacefileForm.getId() + "'";
		Session session = this.getSession();
		try
		{
			session.createQuery(hql).executeUpdate();
		}
		catch(RuntimeException re)
		{
		        logger.error("deleteYfPlacefiles failed");	
			throw re;
		}
		finally
		{
			if (session != null)
			{
				releaseSession(session);
			}
		}
       }

	public List searchYfPlacefile(YfPlacefileForm yfPlacefileForm,PageBean pageBean){  
	    List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT t.*,p.*,b.name project_org_name FROM yf_placefile t left join yf_project p on t.project_no = p.project_no left join department b on p.project_org = b.id  WHERE 1=1 ");
		sql = getStringBuffer(yfPlacefileForm,sql,args);
		return getPageInfo(pageBean,sql.toString(),args);
	}

	/**
	 * 查询条件的封装 
	 * @param departmentForm 
	 * @return
	 */
	private StringBuffer getStringBuffer(YfPlacefileForm yfPlacefileForm,StringBuffer buf,List args){
		/* 需要加入查询条件时封装
		*/
		
           				if(StringUtils.isNotBlank(yfPlacefileForm.getProjectNo())){
		    buf.append(" and t.PROJECT_NO like ? ");
			args.add("%" + yfPlacefileForm.getProjectNo().trim() + "%");
		}
           			 if(StringUtils.isNotBlank(yfPlacefileForm.getId())){
         		     	buf.append(" and t.id = ? ");
         				args.add(yfPlacefileForm.getId());
         			}              
         		   if (StringUtils.isNotBlank(yfPlacefileForm.getProjectName())) {
         				buf.append(" and p.project_name like ? ");
         				args.add("%" + yfPlacefileForm.getProjectName().trim() + "%");
         			}
         			if (StringUtils.isNotBlank(yfPlacefileForm.getProjectOrg())) {
         				buf.append(" and p.project_org = ? ");
         				args.add(yfPlacefileForm.getProjectOrg());
         			}
         			if(yfPlacefileForm.getInitiateYear()!=null && yfPlacefileForm.getInitiateYear().longValue() !=0 ){
         				buf.append(" and p.initiate_year = ? ");
         				args.add(yfPlacefileForm.getInitiateYear());
         			}
         			buf = CommonUtil.filterData(buf,args);
                				return buf;
	}
	
}
	